package com.richapp.launcher.database.infrared.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.richapp.launcher.database.infrared.InfraredDatabaseHelper;
import com.richapp.launcher.database.infrared.mode.MachineType;

import java.util.List;

public class MachineTypeDao {

    private Context context;
    private InfraredDatabaseHelper helper = null;
    private Dao<MachineType, Integer> dao = null;

    public Dao<MachineType, Integer> getDao() {
        return dao;
    }

    public MachineTypeDao(Context context) {
        this.context = context;
        try {
            helper = InfraredDatabaseHelper.getHelper(this.context);
            dao = helper.getDao(MachineType.class);
            dao.setObjectCache(false);
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public List<MachineType> getList() {
        List<MachineType> list = null;
        try {
            list = dao.queryBuilder().orderBy("machineTypeId", true).query();
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
        return list;
    }

    public void add(MachineType machineType) {
        try {
            Dao.CreateOrUpdateStatus status = dao.createOrUpdate(machineType);
            Log.e(MachineTypeDao.class.getName(), status.isCreated() + "-" + status.isUpdated());
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

    public void deleteAll() {
        try {
            DeleteBuilder deleteBuilder = dao.deleteBuilder();
            deleteBuilder.delete();
        } catch (Exception e) {
            Log.e("error", e.getMessage());
        }
    }

}
